package cn.yeyiu.netonlinevideo.parser.video;

import android.util.Base64;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.ThreadUtil;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoInfo;

public class RiYeParser extends Parser {

    Map<String, String> header = new HashMap<>();

    public RiYeParser(SourceData source) {
        super(source);
        ThreadUtil.run(this::initUser);
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String vtype = getArgv(type, "vtype");
        JSONObject data = new JSONObject();
        data.put("type", vtype);
        data.put("sub_category_id", 1);
        data.put("page", pageIndex + 1);
        String html = NetUtil.postByUrl(source.url + "movie/index", data, false, header);

        return getJson(html);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        JSONObject jo = (JSONObject) data;
        JSONArray array = jo.getJSONObject("data").getJSONArray("list");

        List<MediaData> videoInfos = new ArrayList<>();
        MediaData mediaData;
        VideoInfo videoInfo;
        for (int i = 0; i < array.size(); ++i) {
            JSONObject ob = array.getJSONObject(i);
            videoInfo = new VideoInfo();
            mediaData = new MediaData();

            videoInfo.code = source.code;
            videoInfo.type = type;
            videoInfo.title = ob.getString("title");
            videoInfo.picSrc = ob.getString("cover");
            videoInfo.picSrc = videoInfo.picSrc.replaceFirst("(?<=(//))(.+?)(?=/)", "ryimg.hubeixny.net");
            videoInfo.info = ob.getString("id");;

            mediaData.wrap(videoInfo);
            videoInfos.add(mediaData);
        }
        return videoInfos;
    }

    JSONObject getJson(String html) {
        html = html.replaceAll("36aa8bb5b88a03d405bc1fd425d48a9f", "");
        String ooj = new String(Base64.decode(html, Base64.DEFAULT));
        return JSONObject.parseObject(ooj);
    }

    void initUser() {
        JSONObject data = new JSONObject();

        String did = UUID.randomUUID().toString();
        data.put("device_id", did);
        String html = NetUtil.postByUrl(source.url + "app/init_userinfo", data, false, new HashMap<String, String>() {{
            put("device_id", did);
            put("channel_id", "default_channel");
        }});
        JSONObject json = getJson(html).getJSONObject("data");
        String uid = json.getString("user_id");
        String token = json.getString("token");

        header.put("device_id", did);
        header.put("user_id", uid);
        header.put("token", token);
        header.put("channel_id", "default_channel");
    }

    @Override
    public Object buildData(MediaData mediaData) {
        VideoInfo videoInfo = (VideoInfo) mediaData.data;
        JSONObject data = new JSONObject();
        // info is id
        data.put("id", videoInfo.info);
        String html = NetUtil.postByUrl(source.url + "movie/play", data, false, header);
        JSONObject json = getJson(html);
        String url = json.getJSONObject("data").getString("url");
        if ("".equals(url)) {
            initUser();
            html = NetUtil.postByUrl(source.url + "movie/play", data, false, header);
            json = getJson(html);
            url = json.getJSONObject("data").getString("url");
        }
        videoInfo.videoSrc = url;
        return videoInfo;
    }
}
